package UI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Shell to be displayed for the user in order to let them enter their player names.
 */
public class EnterNameShell extends Shell
{
	private PlayerNameSelectedCallback callback;
	
	public EnterNameShell(PlayerNameSelectedCallback callback)
	{
		super(SWTUtil.getDisplay());
		
		this.callback = callback;
		
		FormLayout formLayout = new FormLayout();
		formLayout.marginWidth = 30;
		formLayout.marginHeight = 30;
		
		setLayout(formLayout);
		setMinimumSize(400, 250);
		
		setText("Please enter names");
		
		/////////////// 1 ///////////////////////
		
		Label name1Label = new Label(this, SWT.LEFT);
		name1Label.setText("Player 1:");
		
		FormData formData = new FormData();
		formData.left = new FormAttachment(0, 0);
		formData.top = new FormAttachment(0, 0);
		formData.width = 100;
		formData.height = 20;
		
		name1Label.setLayoutData(formData);
		
		final Text name1Text = new Text(this, SWT.SINGLE | SWT.BORDER);
		
		formData = new FormData();
		formData.left = new FormAttachment(name1Label, 0, SWT.TRAIL);
		formData.top = new FormAttachment(name1Label, -5, SWT.TOP);
		formData.right = new FormAttachment(100, 0);
		formData.height = 20;
		
		name1Text.setLayoutData(formData);
		
		////////////////// 2 //////////////////////
		
		Label name2Label = new Label(this, SWT.LEFT);
		name2Label.setText("Player 2:");
		
		formData = new FormData();
		formData.left = new FormAttachment(0, 0);
		formData.top = new FormAttachment(name1Label, 20, SWT.BOTTOM);
		formData.width = 100;
		formData.height = 20;
		
		name2Label.setLayoutData(formData);
		
		final Text name2Text = new Text(this, SWT.SINGLE | SWT.BORDER);
		
		formData = new FormData();
		formData.left = new FormAttachment(name2Label, 0, SWT.TRAIL);
		formData.top = new FormAttachment(name2Label, -5, SWT.TOP);
		formData.right = new FormAttachment(100, 0);
		formData.height = 20;
		
		name2Text.setLayoutData(formData);
		
		////////////////// OK Button //////////////////////////
		
		final Button okButton = new Button(this, SWT.PUSH);
		okButton.setText("Play!");
		setDefaultButton(okButton);
		
		formData = new FormData();
		formData.right = new FormAttachment(100, 0);
		formData.bottom = new FormAttachment(100, 0);
		formData.width = 100;
		formData.height = 35;
		
		okButton.setLayoutData(formData);
		
		okButton.addSelectionListener(new SelectionAdapter()
		{
			@Override
			public void widgetSelected(SelectionEvent e)
			{
				String player1Name = name1Text.getText();
				String player2Name = name2Text.getText();
				
				if (player1Name.length() == 0) player1Name = null;
				if (player2Name.length() == 0) player2Name = null;
				
				EnterNameShell.this.callback.onPlayerNamesSelected(EnterNameShell.this, player1Name, player2Name);
			}
		});
		
		pack();
	}
	
	@Override
	protected void checkSubclass()
	{
		// Empty on purpose. Makes subclassing Shell possible.
	}
}
